﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Things.Common;

namespace Things.Web.Components
{
    public partial class GoodsInformation : System.Web.UI.UserControl
    {
        public Guid GoodsPriceID
        {
            get
            {
                Guid temp = Guid.Empty;
                try
                {
                    if (Request.QueryString["id"] != null)
                    {
                        bool flag = Guid.TryParse(Request.QueryString["id"].Trim(), out temp);
                        if (flag == false)
                        {
                            temp = Guid.Empty;
                        }
                    }
                }
                catch
                {
                    temp = Guid.Empty;
                }
                return temp;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
  

        protected void BindData()
        {
            Things.BLL.GoodsPrice gpBLL = new BLL.GoodsPrice();
            Things.Model.GoodsPrice gpModel = gpBLL.GetModel(GoodsPriceID);
            if (gpModel != null)
            {
                this.lblStandPrice.Text = gpModel.GoodsStandPrice.ToString();
                this.lblCurrentPrice.Text = gpModel.GoodsCurrentPrice.ToString();

                Things.BLL.Goods gBLL = new BLL.Goods();
                Things.Model.Goods gModel = gBLL.GetModel(gpModel.GoodsID);
                if (gModel != null)
                {
                    Session[ConstantString.SessionImages] = gModel.GoodsImage;
                    this.imgGoodsImage.ImageUrl = "../DisplayImage.aspx";
                    this.lblGoodsTitle.Text = gModel.GoodsName;
                    this.lblWeight.Text = gModel.GoodsWeight;
                    this.lblNotice.Text = gModel.GoodsNotice;
                }
            }
        }

        protected void lbtBuy_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtCount.Text.Trim().Length == 0)
            {
                strErr += "购买数量不能为空！\\n";
            }
            int goodsCount = 1;
            if (int.TryParse(this.txtCount.Text.Trim(), out goodsCount) == false)
            {
                strErr += "购买数量只能是整数！\\n";
            }
            if (strErr != "")
            {
                MessageBox.Show(this.Page, strErr);
                return;
            }

            Things.ThingsWeb.PageUtility pu = new Things.ThingsWeb.PageUtility();
            pu.Add(GoodsPriceID, goodsCount);
            Response.Redirect("../GoodsOrders/GoodsOrder.aspx");
        }

        protected void lbtMark_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtCount.Text.Trim().Length == 0)
            {
                strErr += "购买数量不能为空！\\n";
            }
            int goodsCount = 1;
            if (int.TryParse(this.txtCount.Text.Trim(), out goodsCount) == false)
            {
                strErr += "购买数量只能是整数！\\n";
            }
            if (strErr != "")
            {
                MessageBox.Show(this.Page, strErr);
                return;
            }

            Things.ThingsWeb.PageUtility pu = new Things.ThingsWeb.PageUtility();
            pu.Add(GoodsPriceID, goodsCount);

            Response.Redirect("GoodsCart.aspx");
        }
    }
}